package com.xsoft.demo4;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class LoginActivity extends AppCompatActivity {
    private static final String PREF_NAME = "account";
    private static final String KEY_PHONE_NUM = "phone_num";
    private static final String KEY_PSD = "psd";
    private static final String KEY_PSD_SWITCH = "psd_switch";
    private static final String VALUE_VERIFY_CODE = "891230";
    private RadioGroup rbGroup;
    private Spinner loginTypeSpinner;
    private EditText phoneNumEdt;
    private EditText passwordEdt;
    private EditText verifyCodeEdt;
    private Switch psdSwitch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        rbGroup = findViewById(R.id.rb_group);
        rbGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.psd_rb) {
                    findViewById(R.id.password_msg).setVisibility(View.VISIBLE);
                    findViewById(R.id.password).setVisibility(View.VISIBLE);
                    findViewById(R.id.psd_switch_msg).setVisibility(View.VISIBLE);
                    findViewById(R.id.psd_switch).setVisibility(View.VISIBLE);
                    findViewById(R.id.verify_code_msg).setVisibility(View.GONE);
                    findViewById(R.id.verify_code).setVisibility(View.GONE);
                    findViewById(R.id.verify_code_btn).setVisibility(View.GONE);
                } else if (checkedId == R.id.verify_code_rb) {
                    findViewById(R.id.password_msg).setVisibility(View.GONE);
                    findViewById(R.id.password).setVisibility(View.GONE);
                    findViewById(R.id.psd_switch_msg).setVisibility(View.GONE);
                    findViewById(R.id.psd_switch).setVisibility(View.GONE);
                    findViewById(R.id.verify_code_msg).setVisibility(View.VISIBLE);
                    findViewById(R.id.verify_code).setVisibility(View.VISIBLE);
                    findViewById(R.id.verify_code_btn).setVisibility(View.VISIBLE);
                }
            }
        });
        loginTypeSpinner = findViewById(R.id.login_type_spinner);
        List<String> list = new ArrayList<>();
        list.add("老师");
        list.add("学生");
        list.add("学号姓名");
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, list);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        loginTypeSpinner.setAdapter(adapter);
        loginTypeSpinner.setSelection(2);
        phoneNumEdt = findViewById(R.id.phone_number);
        phoneNumEdt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (phoneNumEdt.getText().toString().trim().length() >= 11) {
                    hideSoftInputFromWindow(phoneNumEdt);
                }
            }
        });
        passwordEdt = findViewById(R.id.password);
        verifyCodeEdt = findViewById(R.id.verify_code);
        findViewById(R.id.verify_code_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(LoginActivity.this)
                        .setTitle("验证码")
                        .setMessage("您的验证码为：" + VALUE_VERIFY_CODE)
                        .show();
            }
        });
        psdSwitch = findViewById(R.id.psd_switch);
        findViewById(R.id.login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phoneNumber = phoneNumEdt.getText().toString().trim();
                if (verifyCodeEdt.getVisibility() == View.GONE) {
                    String password = passwordEdt.getText().toString().trim();
                    if (!new DataBaseHelper(LoginActivity.this).loginAccount(phoneNumber, password)) {
                        Toast.makeText(LoginActivity.this, "密码错误", Toast.LENGTH_LONG).show();
                        return;
                    } else {
                        Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_LONG).show();
                    }
                } else {
                    String verifyCode = verifyCodeEdt.getText().toString().trim();
                    if (!TextUtils.equals(verifyCode, VALUE_VERIFY_CODE)) {
                        Toast.makeText(LoginActivity.this, "验证码输入错误", Toast.LENGTH_LONG).show();
                        return;
                    } else {
                        Toast.makeText(LoginActivity.this, "验证码登录成功", Toast.LENGTH_LONG).show();
                    }
                }
                setValue(KEY_PHONE_NUM, phoneNumEdt.getText().toString().trim());
                setValue(KEY_PSD, passwordEdt.getText().toString().trim());
                setSwitchState(KEY_PSD_SWITCH, psdSwitch.isChecked());
            }
        });
        findViewById(R.id.forget_psd).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phoneNumber = phoneNumEdt.getText().toString().trim();
                if (TextUtils.isEmpty(phoneNumber)) {
                    Toast.makeText(LoginActivity.this, "请输入手机号", Toast.LENGTH_SHORT).show();
                    return;
                }
                startActivity(new Intent(LoginActivity.this, ForgetPasswordActivity.class).putExtra("phone", phoneNumber));
            }
        });
        if (getSwitchState(KEY_PSD_SWITCH)) {
            phoneNumEdt.setText(getValue(KEY_PHONE_NUM));
            passwordEdt.setText(getValue(KEY_PSD));
            psdSwitch.setChecked(getSwitchState(KEY_PSD_SWITCH));
        }
    }

    private void hideSoftInputFromWindow(View view) {
        InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        manager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    private void setValue(String key, String value) {
        SharedPreferences preferences = getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        Editor editor = preferences.edit();
        editor.putString(key, value);
        editor.commit();
    }

    private String getValue(String key) {
        SharedPreferences preferences = getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        return preferences.getString(key, null);
    }

    private void setSwitchState(String key, boolean value) {
        SharedPreferences preferences = getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        Editor editor = preferences.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    private boolean getSwitchState(String key) {
        SharedPreferences preferences = getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        return preferences.getBoolean(key, false);
    }
}